package cn.edu.hpu.service;


import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.dao.MessageDao;
import cn.edu.hpu.pojo.Message;
import cn.edu.hpu.util.Pager;

public class MessageService {
	private MessageDao messageDao;
	public void save(Message message){
		messageDao.save(message);
	}
	public Message findA(String ctdid){
		return messageDao.findByUnique("ctdid", ctdid);
	}
	public void update(Message message){
		messageDao.save(message);
	}
	
	public List<Message> findAllMessage(){
		String hql = "from Message";
		//return messageDao.findAll();
		return messageDao.find(hql);
	}
	public void deletemessage(String id){
		String hql = "from Message where ctdid = ?";
		Message m = messageDao.findUnique(hql, id);
		messageDao.del(m.getId());
		
	}

	public long findMessageCount(){
		String hql = "select count(*) from Message";
		Object object = messageDao.findUnique(hql);
		return (Long)object;
	}
	@SuppressWarnings("unchecked")
	public Pager<Message> findByPager(int num){
		int totalcount = (int) findMessageCount();
		Pager<Message>  p = new Pager<Message>(num,totalcount);
		String hql = "from Message";
		Query  query = messageDao.getSession().createQuery(hql);
		query.setFirstResult(p.getStartlinenum());
		query.setMaxResults(p.getPagesize());
		List<Message> list = query.list();
		p.setResult(list);
		return p;
	}

	
	
	
	@Autowired
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}
	
}
